/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#include "Local.hpp"

namespace spelprog2 {
namespace net {

Local::Local() : Net(true)
{
    UserId clientId = getVars().addUser();
    assert(clientId == FIRST_FREE_ID);

    getVars().setVar(FIRST_FREE_ID, "player", "0");
}

void Local::poll()
{
}

void Local::run()
{
    poll();
}

UserId Local::getMyClientId() const
{
    return FIRST_FREE_ID;
}

void Local::sendToUser(const spelprog2::Message & msg, UserId receiver)
{
    assert(receiver == SERVER_ID || receiver == FIRST_FREE_ID); //force correct semantics for 1 user server

    spelprog2::MessagePtr newMsg = spelprog2::MessagePtr(msg.New());
    newMsg->CopyFrom(msg);
    NetMessageInfo info;

    info.net = this;
    info.origin = receiver == SERVER_ID? FIRST_FREE_ID : SERVER_ID;
    postMsg(newMsg, info);
}

void Local::sendToAll(const spelprog2::Message & msg)
{
    sendToUser(msg, FIRST_FREE_ID);
}

}; //namespace net
}; //namespace spelprog2
